import apple.laf.JRSUIUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jack on 18/3/31.
 */
public class TraversalTree {
    private List<List<Integer>> levelorder = new ArrayList<List<Integer>>();
    public List<List<Integer>> levelOrder(TreeNode root) {

        addele(root);

        return levelorder;

    }

    private void addele(TreeNode node){
        if(node == null){
            return;
        }
        List<TreeNode> nodes = new ArrayList<>();
        nodes.add(node);
        addele(nodes);
    }

    private void addele(List<TreeNode> nodelist){
        if(nodelist.size() == 0){
            return;
        }
        List<Integer> levels = new ArrayList();
        List<TreeNode> nextLevels = new ArrayList<>();
        for(TreeNode node : nodelist){
            if(node.left != null){
                nextLevels.add(node.left);
            }
            if(node.right != null){
                nextLevels.add(node.right);
            }
            levels.add(node.val);
        }
        levelorder.add(levels);
        addele(nextLevels);
    }
}